﻿package com.singlenotion.display.preloaders
{
	import com.leebrimelow.drawing.Arc;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import gs.TweenLite;
	
	/**
	 * ...
	 * @author Adam Jowett (Single Notion)
	 */
	public class RingPreloader extends MovieClip
	{
		public var glow:MovieClip;
		public var logo:MovieClip;
		
		private var _targetAlpha:int;
		private var _logo:Class;
		private var _preLoaderSize:Number;
		private var _preLoader:Sprite;
		private var _logoClip:MovieClip;
		
		public function RingPreloader($logo:Class = null, $ringSize:Number = 24) 
		{
			_logo = $logo;
			_preLoaderSize = $ringSize;
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}
		
		private function init($e:Event):void 
		{
			if (_logo) {
				_logoClip = new _logo();
				addChild(_logoClip);
			}
			
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			_preLoader = new Sprite();
			_preLoader.graphics.lineStyle(1, 0x00CC33, 1);
			addChild(_preLoader);
			
			startGlow();
		}
		
		private function startGlow():void
		{
			_targetAlpha = (_targetAlpha == 1) ? 0 : 1;
			TweenLite.to(glow, 0.6, { alpha:_targetAlpha, onComplete:startGlow } );
		}
		
		public function destroy():void
		{
			if (_logoClip != null) removeChild(_logoClip);
			if (parent != null) parent.removeChild(this);
		}
		
		public function updateProgress($val:Number):void
		{
			Arc.draw(_preLoader, _preLoaderSize, 0, _preLoaderSize, (360 * $val));
			
			if ($val == 1) {
				_preLoader.graphics.clear();
				_preLoader.graphics.lineStyle(1, 0x00CC33, 1);
			}
		}
	}
}